package com.nuit.cxl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Test03 {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//        Class c1 = Class.forName("com.nuit.cxl.Person");
//        Annotation annotation = (TableName)c1.getAnnotation(TableName.class);
//        TableName tableName = (TableName) annotation;
//        System.out.println(tableName.value());
//
//        Field fielldId = c1.getDeclaredField("id");
//        System.out.println(fielldId);
//        Annotation annotation1 = fielldId.getAnnotation(AnnotationField.class);
//        AnnotationField field = (AnnotationField) annotation1;
//        System.out.println(field.name());
//        System.out.println(field.type());

        Test03 t = new Test03();
        System.out.println(t.genSelectByIdSql("com.nuit.cxl.Person", 1));
    }

    public String genSelectByIdSql(String className, int id) throws ClassNotFoundException, NoSuchFieldException {
        //获取类的对象
        Class c1 = Class.forName(className);
        //获取到 类上面的注解（在这里这个注解代表的就是表名）
        Annotation annotation = c1.getAnnotation(TableNameTest.class);
        TableNameTest tableName = (TableNameTest) annotation;
        //获取到id 这个属性
        Field fielldId = c1.getDeclaredField("id");
        //获取到id属性上面的 注解
        Annotation annotation1 = fielldId.getAnnotation(AnnotationField.class);
        AnnotationField field = (AnnotationField) annotation1;
        //根据属性上面的注解，就可以获取注解的内容，譬如他的id，他的type
        String sqlString = "select * from " + tableName.value() + " where " +  field.name() + "=" + id;
        return sqlString;

        // insert  into  tableName (id, name) values ( 对象的id， 对象的名字);
    }
}
